<template>
	<view>
		<!-- 搜索 -->
		<view class="search-box">
			<uni-search-bar radius="100" @input="input" clearButton="auto"  cancelButton="none" />
		</view>
		
		<!-- 搜索建议列表 -->
		<view class="sugg-list" v-if="searchResultsList.length !== 0">
			<view class="sugg-item" v-for="(item,i) in searchResultsList" :key="i" @click="gotoDetail(item)">
				<view class="goods-name">{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		
		<!-- 搜索历史 -->
		<view class="history-box" v-else>
			
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="cleanHistory"></uni-icons>
			</view>
			
			<view class="history-list">
				<uni-tag :text="item" v-for="(item,i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
			</view>
		</view>
		
	
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 防抖处理 延时器的 timerId
				timer: null,
				keyword: '',
				// 搜索结果列表
				searchResultsList: [],
				// 搜索历史列表
				historyList: []
			};
		},
		computed:{
			histories (){
				return [...this.historyList].reverse()
			}
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('keyword') || '[]')
		},
		methods: {
			input(e) {
				// e是输入框中的内容
				// 清除延时器
				clearTimeout(this.timer)
				
				// 设置延时器
				this.timer = setTimeout(()=>{
					// 如果5毫秒内没有触发新的事件，则为搜索关键词赋值
					this.keyword = e
					
					this.getSearchResultsList()
					
					
				},500)
			},
			async getSearchResultsList(){
				// 判断搜索关键词是是否为空
				if(this.keyword.length === 0) {
					this.searchResultsList = []
					return
				}
				
				const {data : res} = await uni.$http.get('/api/public/v1/goods/qsearch',{query : this.keyword})
				if(res.meta.status !== 200 ) return uni.$showMsg()
				this.searchResultsList = res.message
				
				this.saveSearchHistory()
				
			},
			gotoDetail(item) {
				uni.navigateTo({
					url: '/subpkg/goods_detail/goods_detail?cid=' + item.goods_id
				})
			},
			saveSearchHistory(){
				// this.historyList.unshift(this.keyword)
				// 用push 需要配合计算属性
				// this.historyList.push(this.keyword)
				
				// 解决搜索历史重复问题 set set集合里面的数据是唯一的	
				
				// 将 historyList 数组 转化为set对象
				const set = new Set( this.historyList )
				set.delete(this.keyword)
				set.add(this.keyword)
				// 将set对象转化为数组
				this.historyList = Array.from(set)
				
				//调用 uni.setstorageSync(key，value)将搜索历史记录持久化存储到本地
				uni.setStorageSync('keyword',JSON.stringify(this.historyList))
				
			},
			cleanHistory() {
				this.historyList = []
				uni.setStorageSync('keyword','[]')
			},
			gotoGoodsList(keyword){
				uni.navigateTo({
					url: '/subpkg/goods_list/goods_list?query=' + keyword
				})
			}
			
			
		}
	}
</script>

<style lang="scss">
.search-box {
	position: sticky;
	top: 0;
	z-index: 999;
}

.sugg-list {
	padding: 0 5px;
	
	.sugg-item {
		font-size: 12px;
		padding: 13px 0;
		border-bottom: 1px solid #efefef;
		display: flex;
		align-items: center;
		justify-content: space-around;
		
		.goods-name {
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
			margin-right: 3px;
		}
	}
}

.history-box {
	padding: 0 5px;
	
	.history-title {
		height: 40px;
		font-size: 13px;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1px solid #efefef;
	}
	
	.history-list {
		display: flex;
		flex-wrap: wrap;
		
		.uni-tag {
			display: block;
			background-color: #f9f9f9;
			margin-right: 10px;
			margin-top: 10px;
			border: 0;
			color: #000000;
			padding: 10px 15px;
			}
	}
}



</style>
